package com.qf.admin.controller;


import cn.hutool.db.meta.Table;
import com.qf.admin.annotation.ProcessInfo;

import com.qf.admin.domain.Orders;

import com.qf.admin.model.Results;
import com.qf.admin.pojo.QueryOrder;
import com.qf.admin.service.IOrdersService;
import com.qf.commons.TableData;
import com.qf.pojo.ProcessLog;

import org.springframework.web.bind.annotation.*;

import javax.xml.transform.Result;
import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin("*")
@RequestMapping("/order")
public class OrdersController {


    private IOrdersService OrdersService;

    public OrdersController(IOrdersService OrdersService) {
        this.OrdersService = OrdersService;
    }

    @GetMapping
    public TableData<Orders> getAllGoods(QueryOrder queryOrder,
                                         @RequestParam(defaultValue = "0") Integer offset,
                                         @RequestParam(defaultValue = "10") Integer limit) {

        TableData<Orders> allOrders = OrdersService.getAllOrders(queryOrder, offset, limit);

        return allOrders;

    }
    @GetMapping("/ProcessLog")
    public TableData<ProcessLog> dataStatistics(  @RequestParam(defaultValue = "0") Integer offset,
                                                  @RequestParam(defaultValue = "10") Integer limit){
        TableData<ProcessLog> processLog= OrdersService.getProcessLog(offset, limit);
        return processLog;
    }



    // 根据Id查询
    @GetMapping("/{id}")
    @ProcessInfo("根据订单号查看详情查询")
    public Orders getUserById(@PathVariable("id") Integer id) {
        Orders orderUserById = OrdersService.getOrderUserById(id);
        return orderUserById;
    }

    //根据u_id获取用户可选的收货地址
    @GetMapping("/uid/{id}")
    @ProcessInfo("根据u_id获取用户可选的收货地址")
    public List<Orders> getUserAddress(@PathVariable("id") Integer id) {
        System.out.println("根据u_id获取用户可选的收货地址");
        List<Orders> ordersList = new ArrayList<>();
        List<Orders> ordersUserAddress = OrdersService.getUserAddress(id);
        return ordersUserAddress;

    }
    //更换收货地址
    //根据a_id查询收货地址，再返回前端渲染
    @GetMapping("/{aid}/{uid}")
    @ProcessInfo("更换收货地址")
    public Results updateAddress(@PathVariable("aid")Integer aid, @PathVariable("uid") Integer uid){
        System.out.println("update.....");
        Results results=new Results();
        try{
            OrdersService.updateAddress(aid,uid);
            results.setCode(1);
            results.setMsg("success");
        }catch (Exception ex){
            ex.printStackTrace();
            results.setCode(-1);
            results.setMsg("fail");
        }
        return  results;
    }







}
